<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24"> </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->

    <a-tabs v-model="activeKey" @change="handleChangeTab">
      <a-tab-pane key="notGenerated" tab="未生成">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchNotGenerated">
            <a-row :gutter="24">
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="单据编号">
                  <a-input placeholder="" v-model="orderCode"></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchNotGenerated" icon="search">查询</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- table区域-begin -->
        <div>
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            class="j-table-force-nowrap1"
            :scroll="{ x: true }"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            @change="handleTableChange"
          >
            <span slot="action" slot-scope="text, record">
              <a @click="addPlan(record)">新增</a>
            </span>
          </a-table>
        </div>
      </a-tab-pane>
      <a-tab-pane key="tempSave" tab="暂存中">
        <a-table
          ref="tempSaveTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap1"
          :scroll="{ x: true }"
          :columns="planColumns"
          :dataSource="tempSaveTable.dataSource"
          :pagination="tempSaveTable.ipagination"
          :loading="tempSaveTable.loading"
          :rowSelection="{ selectedRowKeys: tempSaveTable.selectedRowKeys, onChange: onSelectChangeTempSave }"
        >
          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text, record">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
            <img
              v-else
              :src="getImgView(text)"
              :preview="record.id"
              height="25px"
              alt=""
              style="max-width: 80px; font-size: 12px; font-style: italic"
            />
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
            <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">编辑</a>
            <act-apply-btn
              @success="loadData"
              :data-id="record.id"
              :serviceName="serviceName"
              :variables="record"
            ></act-apply-btn>
            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </a-table>
      </a-tab-pane>
      <a-tab-pane key="needEdit" tab="需修改">
        <a-table
          ref="needEditTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap1"
          :scroll="{ x: true }"
          :columns="planColumns"
          :dataSource="needEditTable.dataSource"
          :pagination="needEditTable.ipagination"
          :loading="needEditTable.loading"
          :rowSelection="{ selectedRowKeys: needEditTable.selectedRowKeys, onChange: onSelectChangeNeedEdit }"
        >
          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text, record">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
            <img
              v-else
              :src="getImgView(text)"
              :preview="record.id"
              height="25px"
              alt=""
              style="max-width: 80px; font-size: 12px; font-style: italic"
            />
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
            <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">编辑</a>
            <act-apply-btn
              v-show="record.actStatus == '撤回'"
              @success="loadData"
              :data-id="record.id"
              :serviceName="serviceName"
              :variables="record"
              text="重新提交"
            ></act-apply-btn>
            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </a-table>
      </a-tab-pane>
      <a-tab-pane key="approving" tab="审批中">
        <a-table
          ref="approvingTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap1"
          :scroll="{ x: true }"
          :columns="planColumns"
          :dataSource="approvingTable.dataSource"
          :pagination="approvingTable.ipagination"
          :loading="approvingTable.loading"
          :rowSelection="{ selectedRowKeys: approvingTable.selectedRowKeys, onChange: onSelectChangeApproving }"
        >
          <span slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">编辑</a>

            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </a-table>
      </a-tab-pane>
      <a-tab-pane key="approved" tab="已审批">
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchNotGenerated">
            <a-row :gutter="24">
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="制单日期">
                  <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="createTime_begin"></j-date>
                  <span class="query-group-split-cust"></span>
                  <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="createTime_end"></j-date>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="loadApprovedData" icon="search">查询</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <a-table
          ref="approvedTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap1"
          :scroll="{ x: true }"
          :columns="planColumns"
          :dataSource="approvedTable.dataSource"
          :pagination="approvedTable.ipagination"
          :loading="approvedTable.loading"
          :rowSelection="{ selectedRowKeys: approvedTable.selectedRowKeys, onChange: onSelectChangeApproved }"
        >
          <span slot="action" slot-scope="text, record">
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </a-table>
      </a-tab-pane>
    </a-tabs>

    <tsl-sale-production-plan-modal ref="modalForm" @ok="modalFormOk" />
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import TslSaleProductionPlanModal from './modules/TslSaleProductionPlanModal'
import '@/assets/less/TableExpand.less'
import { getAction, httpAction } from '@api/manage'
import ActApplyBtn from '@views/flowable/components/ActApplyBtn.vue'

import ActHistoricDetailBtn from '@views/flowable/components/ActHistoricDetailBtn.vue'
import { deepClone, getDate } from '../../utils'

export default {
  name: 'TslSaleProductionPlanList',
  mixins: [JeecgListMixin],
  components: {
    TslSaleProductionPlanModal,
    ActApplyBtn,
    ActHistoricDetailBtn,
  },
  data() {
    return {
      description: '营销生产计划单管理页面',
      serviceName: 'saleProductionPlanService',
      createTime_begin: '',
      createTime_end: '',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '单据类型',
          align: 'center',
          dataIndex: 'documentType_dictText',
        },
        {
          title: '单据编号',
          align: 'center',
          dataIndex: 'code',
          sorter: (a, b) => a.code.localeCompare(b.code, 'en'),
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy',
        },
        {
          title: '创建日期',
          align: 'center',
          sorter: true,
          dataIndex: 'createTime',
          sorter: (a, b) => a.createTime.localeCompare(b.createTime, 'en'),
        },

        // {
        //   title:'状态',
        //   align:"center",
        //   dataIndex: 'status'
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      planColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },

        {
          title: '单据编号',
          align: 'center',
          dataIndex: 'code',
          sorter: (a, b) => a.code.localeCompare(b.code, 'en'),
        },
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'orderCode',
          sorter: (a, b) => a.orderCode.localeCompare(b.orderCode, 'en'),
        },
        {
          title: '订单类型',
          align: 'center',
          dataIndex: 'documentType_dictText',
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createBy',
        },
        {
          title: '创建日期',
          align: 'center',
          sorter: true,
          dataIndex: 'createTime',
          sorter: (a, b) => a.createTime.localeCompare(b.createTime, 'en'),
        },
        {
          title: '流程状态',
          align: 'center',
          dataIndex: 'actStatus',
        },
        {
          title: '待处理节点',
          align: 'center',
          dataIndex: 'taskName',
        },

        // {
        //   title:'状态',
        //   align:"center",
        //   dataIndex: 'status'
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      approvedTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['1000', '3000', '10000'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      approvingTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['1000', '3000', '10000'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      needEditTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['1000', '3000', '10000'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      tempSaveTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 30,
          pageSizeOptions: ['1000', '3000', '10000'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      url: {
        waitGenerateList: '/review/bussInternationalTradeOrders/listWaitProductionPlan',
        list: '/sale/tslSaleProductionPlan/list',
        delete: '/sale/tslSaleProductionPlan/delete',
        deleteBatch: '/sale/tslSaleProductionPlan/deleteBatch',
        exportXlsUrl: '/sale/tslSaleProductionPlan/exportXls',
        importExcelUrl: 'sale/tslSaleProductionPlan/importExcel',
      },
      orderCode: '',
      notGeneratedList: [],
      activeKey: 'notGenerated',
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.createTime_begin = getDate('day', -7)
    this.createTime_end = getDate('day', 0)
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    onSelectChangeNeedEdit(selectedRowKeys, selectedRows) {
      this.needEditTable.selectedRowKeys = selectedRowKeys
    },
    onSelectChangeApproving(selectedRowKeys, selectedRows) {
      this.approvingTable.selectedRowKeys = selectedRowKeys
    },
    onSelectChangeApproved(selectedRowKeys, selectedRows) {
      this.approvedTable.selectedRowKeys = selectedRowKeys
    },
    onSelectChangeTempSave(selectedRowKeys, selectedRows) {
      this.tempSaveTable.selectedRowKeys = selectedRowKeys
    },
    addPlan(record) {
      this.$refs.modalForm.add(record)
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
    },
    searchNotGenerated() {
      const db = [...this.notGeneratedList]
      this.dataSource = db.filter((item) => item.code.includes(this.orderCode))
    },
    initDictConfig() {},
    handleChangeTab() {
      switch (this.activeKey) {
        case 'notGenerated':
          this.loadNotGeneratedData()
          break
        case 'tempSave':
          this.loadTempSaveData()
          break
        case 'needEdit':
          this.loadNeedEditData()
          break
        case 'approving':
          this.loadApprovingData()
          break
        case 'approved':
          this.loadApprovedData()
          console.log('approved')
          break
        case 'closed':
          this.loadClosedData()
          console.log('closed')
          break
        default:
          break
      }
    },
    loadNotGeneratedData() {
      this.loading = true
      getAction(this.url.waitGenerateList)
        .then((res) => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
            this.notGeneratedList = deepClone(this.dataSource)
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    loadTempSaveData() {
      this.tempSaveTable.loading = true
      this.queryParam.status = -2
      getAction(this.url.list, this.queryParam)
        .then((res) => {
          if (res.success) {
            this.tempSaveTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.tempSaveTable.ipagination.total = res.result.total
            } else {
              this.tempSaveTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.tempSaveTable.loading = false
        })
    },
    loadNeedEditData() {
      this.needEditTable.loading = true
      this.queryParam.status = -1
      getAction(this.url.list, this.queryParam)
        .then((res) => {
          if (res.success) {
            this.needEditTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.needEditTable.ipagination.total = res.result.total
            } else {
              this.needEditTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.needEditTable.loading = false
        })
    },
    loadApprovingData() {
      this.approvingTable.loading = true
      this.queryParam.status = 0
      getAction(this.url.list, this.queryParam)
        .then((res) => {
          if (res.success) {
            this.approvingTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.approvingTable.ipagination.total = res.result.total
            } else {
              this.approvingTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.approvingTable.loading = false
        })
    },
    loadApprovedData() {
      this.approvedTable.loading = true
      let param = {
        status: 999,
        createTime_begin: this.createTime_begin,
        createTime_end: this.createTime_end,
      }
      getAction(this.url.list, param)
        .then((res) => {
          if (res.success) {
            this.approvedTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.approvedTable.ipagination.total = res.result.total
            } else {
              this.approvedTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.approvedTable.loading = false
        })
    },
    async loadData() {
      //分别根据单据状态查询结果并将其放在
      this.queryParam = this.getQueryParams()
      this.handleChangeTab()
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'createBy', text: '创建人', dictCode: '' })
      fieldList.push({ type: 'datetime', value: 'createTime', text: '创建日期' })
      fieldList.push({ type: 'string', value: 'code', text: '单据编号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'specialRemark', text: '特殊说明', dictCode: '' })
      fieldList.push({ type: 'int', value: 'status', text: '状态', dictCode: '' })
      this.superFieldList = fieldList
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
